/**
 * Created by chang on 2017/8/25.
 */
$("#quit").click(function () {
    Comm().logout();
});
window.onload = function () {
    var token = localStorage.getItem("token");
    if (!token){
        location.href = '/template/login.html';
    }
    var userId;
    Ajax(1,"/api/user/info",'get').then(function (res) {
        userId = res.data.id;
        if(res.data.headIcon){
            $('.userhead').attr('src',res.data.headIcon);
        }
        for(prop in res.data){
            if(res.data[prop] == null){
                res.data[prop] = '未填写';
            }
        }
        if(res.data.sex == 0){
            $('#sex').text('性別：女');
        }else if(res.data.sex == 1){
            $('#sex').text('性別：男');
        }
        $(".personal-name").html(res.data.nikeName);
        $('#username').text('昵称：'+ res.data.nikeName);
        $('#birthday').text('生日：'+ res.data.birthday);

    });

    //点击编辑信息
    $("#update-info").click(function(){
        $("#mask").css("display","block");
        $('.name').attr('placeholder',$('#username').text().replace('昵称：',''));
        $('.demo-input').attr('placeholder',$('#birthday').text().replace('生日：',''));
    });
   // 点击x关闭遮罩层
    $("#close").click(function(){
        $("#mask").css("display","none");
        $('.name').val('');
        $('.demo-input').val('');
        $("input[type='sex']").removeAttr('checked');
    });
    //点击取消关闭遮罩层
    $(".quxiao").click(function(){
        $("#mask").css("display","none");
        $('.name').val('');
        $('.demo-input').val('');
        $("input[type='sex']").removeAttr('checked');
    });
    $('.update-userinfo').click(function () {
        var arr={};
        arr.sex = $('.update input[name="sex"]:checked ').val();
        if(!$('.name').val()){
            arr.nikeName = $('.name').attr('placeholder');
        }else {
            arr.nikeName = $('.name').val();
        }
        if($('.demo-input').val()==''||$('.demo-input').val()==null){
            arr.birthday = $('.demo-input').attr('placeholder');
        }else {
            arr.birthday = $('.demo-input').val();
        }
        arr.id = userId;
        $("#mask").css("display","none");
        Ajax(1,'/api/user/update','post',JSON.stringify(arr)).then(function (r) {
            if (r.code==0){
                if(arr.sex == 0){
                    $('#sex').text('性別：女');
                }else if(arr.sex == 1){
                    $('#sex').text('性別：男');
                }
                $(".personal-name").html(arr.nikeName);
                $('#username').text('昵称：'+ arr.nikeName);
                $('#birthday').text('生日：'+ arr.birthday)
            }
            $("input[type='sex']").removeAttr('checked');
        });
    });
    $('#upload').change(function () {
       // Ajax(1,'comm/upload/img','post',formData.then(function (r){}
        var formData = new FormData();
        var name = $("#upload").val();
        formData.append("files",$("#upload")[0].files[0]);
        if(name.indexOf("jpg")!=-1 || name.indexOf("png")!=-1 || name.indexOf("gif")!=-1){
            $.ajax({
                url : BaseUrl+ '/comm/upload/img',
                type : 'post',
                data : formData,
                processData : false,
                contentType : false,
                success : function(r) {
                    var headicon = r.data[0]
                    var data = {};
                    data.headIcon = headicon;
                    data.id = userId;
                    if(r.code==0){
                        Ajax(1,'/api/user/update','post',JSON.stringify(data)).then(function (r){
                            if(r.code == 0){
                                alert('上传成功');
                                $('.userhead').attr('src',headicon);
                            }
                        });
                    }
                },
                error : function(r) {
                    alert(r.msg);
                }
            });
        }else {
             alert('您选择的不是图片');
        }
    });
}

